/**
 * Created by Administrator on 2016/10/16.
 */
var express = require("express");
var router = express.Router();
var mysql = require("mysql");
var DateUtil = require('../../lib/utils/DateUtil');
var _ = require("underscore");

var connection = require('../../lib/utils/DbconnectUtil');

/**
 * 查询项目列表
 */
router.get('/', function (req, res, next) {
    //console.log("route: /list");
    var menuId = req.query.menuId || '1';

    var queryContent =  'select ' +
        't_content.id contentId, ' +
        't_content.name contentName, ' +
        't_content.summary contentSummary, ' +
        't_content.type contentType, ' +
        't_content.coverUrl contentCoverUrl, ' +
        't_content.videoUrl contentVideoUrl, ' +
        't_content.updateAt contentUpdateAt, ' +
        't_sub_menu.id menuId, ' +
        't_sub_menu.name menuName ' +
        'from t_content, t_sub_menu ' +
        'where t_content.menu = t_sub_menu.id and t_sub_menu.id in ' +
        '(select t_sub_menu.id from t_sub_menu where t_sub_menu.menu = '+menuId+')';

    connection().query(queryContent, function (error, results) {
        if (error) {
            res.send({code : -1 , message: JSON.stringify(error)});
        } else {
            _.forEach(results,function (result) {
                if (result.contentSummary && result.contentSummary.length > 15) {
                    result.contentSummary = result.contentSummary.substr(0, 15) + '...';
                }

                if (result.contentName && result.contentName.length > 20) {
                    result.contentName = result.contentName.substr(0, 20) + '...';
                }
            });
            
            res.send({code :0 , data : results});
        }
    });
});

/**
 * 查询项目内容
 */
router.get('/:contentId', function (req, res, next) {
    //console.log("route: /content");
    var contentId = req.params.contentId;

    var queryContentSql = 'select ' +
        't_content.id id, ' +
        't_content.name name, ' +
        't_content.summary summary, ' +
        't_content.type type, ' +
        't_content.coverUrl coverUrl, ' +
        't_content.videoUrl videoUrl, ' +
        't_content.updateAt updateAt, ' +
        't_content.stick stick, ' +
        't_content.content content, ' +
        't_sub_menu.id subMenuId, ' +
        't_sub_menu.menu menuId ' +
        'from t_content, t_sub_menu ' +
        'where t_content.menu = t_sub_menu.id and t_content.id = ? ';

    // console.log(queryContentSql);
    connection().query(queryContentSql,[contentId], function (error, results) {
            if (error || results.length == 0) {
                console.log(error);
                res.send({code: -1, message: '查询数据失败'});
            } else {
                res.send({code: 0, data: results[0]});
            }
        });
});

/**
 * 更新项目内容
 */
router.put('/:contentId', function (req, res, next) {
    var content = req.body;
    content.updateAt = content.updateAt ? new Date(content.updateAt) : new Date();
    if (!_.isDate(content.updateAt)) {
        console.log('updateAt is not date :' + content.updateAt);
    }

    connection().query('UPDATE t_content SET name = ?, summary = ?, content = ?, menu = ?, type = ?, coverUrl = ?, videoUrl = ?, updateAt = ?, stick = ? WHERE id = ?',
        [content.name, content.summary, content.content, content.subMenuId, content.type, content.coverUrl, content.videoUrl, content.updateAt, content.stick, content.id], function (err, results) {
            if (err) {
                console.log(err);
                res.send({code: -1, message: '保存数据失败'});
            } else {
                res.send({code: 0, data: content});
            }
        });
});

/**
 * 更新项目内容
 */
router.post('/', function (req, res, next) {
    var content = req.body;
    content.id = new Date().getTime()+ '';
    content.updateAt = content.updateAt ? new Date(content.updateAt) : new Date();
    if (!_.isDate(content.updateAt)) {
        console.log('updateAt is not date :' + content.updateAt);
    }
    console.log('time: '+content.updateAt.getTime())

    connection().query('INSERT INTO  t_content SET id = ?,name = ?, summary = ?, content = ?, menu = ?, type = ?, coverUrl = ?, videoUrl = ?, updateAt = ?, stick = ?',
        [ content.id, content.name, content.summary, content.content, content.subMenuId, content.type, content.coverUrl, content.videoUrl, content.updateAt, content.stick], function (err, results) {
            if (err) {
                console.log(err);
                res.send({code: -1, message: '保存数据失败'});
            } else {
                res.send({code: 0, data: content});
            }
        });
});

/**
 * 删除项目内容
 */
router.delete('/:contentId', function (req, res, next) {
    connection().query('DELETE FROM  t_content WHERE id = ?', [req.params.contentId], function (err, results) {
        if (err) {
            console.log(err);
            res.send({code: -1, message: '删除数据失败'});
        } else {
            res.send({code: 0, data: results});
        }
    });
});

module.exports = router;